package com.jeesite.modules.receipt.web;

import java.util.List;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.receipt.entity.ReceiptHeader;
import com.jeesite.modules.receipt.entity.ReceiptLines;
import com.jeesite.modules.receipt.service.ReceiptHeaderService;

/**
 * 收料单Controller
 * @author 码脑洞
 * @version 2025-04-13
 */
@Controller
@RequestMapping(value = "${adminPath}/receipt/header")
public class ReceiptHeaderController extends BaseController {

	@Autowired
	private ReceiptHeaderService receiptHeaderService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public ReceiptHeader get(Long autoId, boolean isNewRecord) {
		return receiptHeaderService.get(autoId, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("receipt:header:view")
	@RequestMapping(value = {"list", ""})
	public String list(ReceiptHeader receiptHeader, Model model) {
		model.addAttribute("receiptHeader", receiptHeader);
		return "modules/receipt/receiptHeaderList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("receipt:header:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<ReceiptHeader> listData(ReceiptHeader receiptHeader, HttpServletRequest request, HttpServletResponse response) {
		receiptHeader.setPage(new Page<>(request, response));
		Page<ReceiptHeader> page = receiptHeaderService.findPage(receiptHeader);
		return page;
	}
	
	/**
	 * 查询子表数据
	 */
	@RequiresPermissions("receipt:header:view")
	@RequestMapping(value = "linesListData")
	@ResponseBody
	public Page<ReceiptLines> subListData(ReceiptLines receiptLines, HttpServletRequest request, HttpServletResponse response) {
		receiptLines.setPage(new Page<>(request, response));
		Page<ReceiptLines> page = receiptHeaderService.findSubPage(receiptLines);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("receipt:header:view")
	@RequestMapping(value = "form")
	public String form(ReceiptHeader receiptHeader, Model model) {
		model.addAttribute("receiptHeader", receiptHeader);
		return "modules/receipt/receiptHeaderForm";
	}

	/**
	 * 保存数据
	 */
	@RequiresPermissions("receipt:header:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated ReceiptHeader receiptHeader) {
		receiptHeaderService.save(receiptHeader);
		return renderResult(Global.TRUE, text("保存收料单成功！"));
	}
	
	/**
	 * 删除数据
	 */
	@RequiresPermissions("receipt:header:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(ReceiptHeader receiptHeader) {
		receiptHeaderService.delete(receiptHeader);
		return renderResult(Global.TRUE, text("删除收料单成功！"));
	}
	
}